from requests import get
from bs4 import BeautifulSoup

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'}
cms = open('../dict/cms_title.txt', 'r', encoding='utf-8')


def check_source(url):
    try:
        result = get(url, headers=headers)
        if result.status_code != 200:
            return '网站不可访问'
        else:
            soup = BeautifulSoup(result.text, 'lxml')
            for i in cms:
                cmsname = i.strip("\n")
                if cmsname in str(soup):
                    return '该网站存在cms，cms为：', cmsname
    except Exception as e:
        return '输入错误或站点无法访问，输入示例http://www.scitc.com.cn'


def check_robots(url):
    try:
        result = get(url + '/robots.txt', headers=headers)
        if result.status_code != 200:
            return '网站不可访问'
        else:
            soup = BeautifulSoup(result.text, 'lxml')
            for i in cms:
                cmsname = i.strip("\n")
                if cmsname in str(soup):
                    return '该网站存在cms，cms为：', cmsname
    except Exception as e:
        return '输入错误或站点无法访问，输入示例http://www.scitc.com.cn'


def run(url):
    if check_source(url) != None:
        return check_source(url)
    elif check_robots(url) != None:
        return check_robots(url)
    else:
        return '该网站无cms'


'https://www.bjwyds.net    http://www.senhongjs.com.cn'
